#ifndef TIMER_TIMER_H
#define TIMER_TIMER_H

#include "TimerID.h"
#include "utility/MoveOnly.h"
#include "utility/chrono.h"
#include "utility/func.h"

class Timer final {
public:
    Timer(TrivialFunc callback, Utility::timepoint expiration,
          Utility::duration interval, const TimerID &id):
        _callback{std::move(callback)},
        _expiration{expiration},
        _interval{interval},
        _id{id} {}
    ~Timer();

    MOVEONLY(Timer);

    Utility::timepoint expiration() const { return _expiration; }
    TimerID id() const { return _id; }

    bool is_repeat() const { return _interval > Utility::duration::zero(); }
    void restart(Utility::timepoint now) {
        _expiration = 
            is_repeat() ? (now + _interval) : Utility::timepoint::min();
    }
    void run() const { _callback(); }

private:
    TrivialFunc _callback;
    Utility::timepoint _expiration;
    Utility::duration _interval;
    TimerID _id;
};



#endif